<?php

declare (strict_types = 1);

namespace app\store\service\statistics\data;

use app\common\enum\user\balanceLog\Scene;
use app\common\model\RetailOrder;
use app\common\service\BaseService;
use app\store\model\Order as OrderModel;
use app\common\library\helper;
use app\store\model\user\BalanceLog;

/**
 * 近7日走势
 * Class Trade7days
 * @package app\store\service\statistics\data
 */
class Trade7days extends BaseService
{
    /* @var OrderModel $GoodsModel */
    private $OrderModel;

    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
        /* 初始化模型 */
        $this->OrderModel = new OrderModel;
    }

    /**
     * 近7日走势
     * @return array
     */
    public function getTransactionTrend(): array
    {
        // 最近七天日期
        $lately7days = $this->getLately7days();
        return [
            'date' => $lately7days,
            'orderTotal' => $this->getOrderTotalByDate($lately7days),
            'orderTotalPrice' => $this->getOrderTotalPriceByDate($lately7days),
            'retailTotalPrice' => 0,   //获取零售订单近7天的成交额
            'preSaleCardTotal' => 0,  //预售卡订单
            'preSaleCardTotalPrice' => 0,  //预售卡订单金额
        ];
    }


    /**
     * 最近七天日期
     */
    private function getLately7days()
    {
        // 获取当前周几
        $date = [];
        for ($i = 0; $i < 7; $i++) {
            $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        }
        return array_reverse($date);
    }

    /**
     * 获取近7天的零售订单成交额
     */
    public function  getRetailOrderTotalPriceByDate($days): array
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = $this->getRetailOrderTotalPrice($day,$day);
        }
        return $data;
    }

    /**获取零售订单某天的销售额
     * @param $day
     */
    private function getRetailOrderTotalPrice($startDate,$endDate): float
    {
       $condition[] = [
           ['pay_status', '=', 20],
           ['order_status', '=', 30],
           ['is_del', '=', 0]
       ];

        // 设置查询条件
        if (!is_null($startDate) && !is_null($endDate)) {
            $condition[] = [
                ['pay_time', '>=', strtotime($startDate)],
                ['pay_time', '<', strtotime($endDate) + 86400]
            ];
        }
        // 总销售额
        return (new RetailOrder)->where($condition)
            ->sum('price');
    }



    /**
     * 获取某天的总销售额
     * @param null $day
     * @return string
     */
    private function getOrderTotalPrice($day = null)
    {
        return helper::number2($this->OrderModel->getOrderTotalPrice($day, $day));
    }

    /**
     * 获取订单总量 (指定日期)
     * @param $days
     * @return array
     */
    private function getOrderTotalPriceByDate($days): array
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = $this->getOrderTotalPrice($day);
        }
        return $data;
    }


    /**
     * 获取某天的订单量
     * @param null $day
     * @return string
     */
    private function getOrderTotal($day = null)
    {
        return $this->OrderModel->getPayOrderTotal($day,$day);
    }

    /**
     * 获取订单总量 (指定日期)
     * @param $days
     * @return array
     */
    private function getOrderTotalByDate($days): array
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = $this->getOrderTotal($day);
        }
        return $data;
    }


    /**预售卡订单总量(指定日期)
     * @param $days
     */
    private function getPreSaleCardTotal($days): array
    {
        $data = [];
        foreach ($days as $day) {
            $day .= ' 00:00:00';
            $condition = [
                ['scene','=',Scene::GAME],
                ['created_at', '>=', $day],
                ['created_at', '<', date('Y-m-d H:i:s',strtotime($day) + 86400)],
            ];
            $num = BalanceLog::where($condition)->count();
            $data[] = $num;
        }
        return $data;
    }

    /**预售卡订单金额(指定日期)
     * @param $days
     */
    private function getPreSaleCardTotalPrice($days):array
    {
        $data = [];
        foreach ($days as $day) {
            $day .= ' 00:00:00';
            $condition = [
                ['scene','=',Scene::GAME],
                ['created_at', '>=', $day],
                ['created_at', '<', date('Y-m-d H:i:s',strtotime($day) + 86400)],
                ['money', '<', 0],
            ];
            $money = BalanceLog::where($condition)->sum('money');
            $data[] = abs($money);
        }
        return $data;
    }



}